package com.totem.startup;

import com.totem.admin.controller.AdminController;
import com.totem.admin.service.ITAppConfigItemService;
import com.totem.admin.service.ITAppConfigService;
import com.totem.admin.service.ITSysCronService;
import com.totem.admin.vo.VerificationCodeVO;
import com.totem.base.constants.CommonConstants;
import com.totem.base.enums.LoginTypeEnum;
import com.totem.customer.model.TCustomers;
import com.totem.customer.serivce.ITCustomerService;
import com.totem.customer.vo.CustomerCreateVO;
import com.totem.product.service.ITFundService;
import com.totem.product.service.ITSubscribeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class InitialAfterStartup implements ApplicationListener<ApplicationReadyEvent> {
    @Autowired
    private AdminController adminController;
    @Autowired
    private ITCustomerService itCustomerService;
    @Autowired
    private ITSubscribeService itSubscribeService;
    @Autowired
    private ITFundService itFundService;
    @Autowired
    private ITAppConfigService itAppConfigService;
    @Autowired
    private ITAppConfigItemService itAppConfigItemService;
    @Autowired
    private ITSysCronService itSysCronService;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 启动后创建管理员账号
        initAdmin();
        // 初始化认购信息
        itSubscribeService.initData();
        // 初始化基金信息
        itFundService.initData();
        // 初始化配置信息
        itAppConfigService.initData();
        // 加载初始化数据到全局配置缓存
        itAppConfigItemService.initData();
        // 初始化定时任务配置
        itSysCronService.initData();
    }

    private void initAdmin(){
        String defaultPassword = CommonConstants.DEFAULT_PWD;
        String defaultTxPassword = CommonConstants.DEFAULT_TX_PWD;
        String countryCode = "86";

        String adminNickname = CommonConstants.DEFAULT_ADMIN;
        String adminEmail = "admin@ipo.com";
        String adminMobile = "13016180000";
        TCustomers admin = itCustomerService.findByNickname(adminNickname);
        if(admin==null){
            VerificationCodeVO codeVo = new VerificationCodeVO();
            codeVo.setLoginType(LoginTypeEnum.EMAIL.getCode());
            codeVo.setNewCustomer(true);
            codeVo.setEmail(adminEmail);
            adminController.verificationCode(codeVo);

            CustomerCreateVO vo = new CustomerCreateVO();
            vo.setLoginType(LoginTypeEnum.EMAIL.getCode());
            vo.setNickname(adminNickname);
            vo.setEmail(adminEmail);
            vo.setPassword(defaultPassword);
            vo.setRepeatPassword(defaultPassword);
            vo.setTxPassword(defaultTxPassword);
            vo.setRepeatTxPassword(defaultTxPassword);
            vo.setVerificationCode(codeVo.getCode());

            itCustomerService.createCustomer(vo);
        }

        String financialNickname = CommonConstants.DEFAULT_FINANCIAL;
        String financialEmail = "financial@ipo.com";
        String financialMobile = "13016180001";
        TCustomers financial = itCustomerService.findByNickname(financialNickname);
        if(financial==null){
            VerificationCodeVO codeVo = new VerificationCodeVO();
            codeVo.setLoginType(LoginTypeEnum.EMAIL.getCode());
            codeVo.setNewCustomer(true);
            codeVo.setEmail(financialEmail);
            adminController.verificationCode(codeVo);

            CustomerCreateVO vo = new CustomerCreateVO();
            vo.setLoginType(LoginTypeEnum.EMAIL.getCode());
            vo.setNickname(financialNickname);
            vo.setEmail(financialEmail);
            vo.setPassword(defaultPassword);
            vo.setRepeatPassword(defaultPassword);
            vo.setTxPassword(defaultTxPassword);
            vo.setRepeatTxPassword(defaultTxPassword);
            vo.setVerificationCode(codeVo.getCode());
            itCustomerService.createCustomer(vo);
        }

        String operationNickname = CommonConstants.DEFAULT_OPERATION;
        String operationEmail = "operation@ipo.com";
        String operationMobile = "13016180002";
        TCustomers operation = itCustomerService.findByNickname(operationNickname);
        if(operation==null){
            VerificationCodeVO codeVo = new VerificationCodeVO();
            codeVo.setLoginType(LoginTypeEnum.EMAIL.getCode());
            codeVo.setNewCustomer(true);
            codeVo.setEmail(operationEmail);
            adminController.verificationCode(codeVo);

            CustomerCreateVO vo = new CustomerCreateVO();
            vo.setLoginType(LoginTypeEnum.EMAIL.getCode());
            vo.setNickname(operationNickname);
            vo.setEmail(operationEmail);
            vo.setPassword(defaultPassword);
            vo.setRepeatPassword(defaultPassword);
            vo.setTxPassword(defaultTxPassword);
            vo.setRepeatTxPassword(defaultTxPassword);
            vo.setVerificationCode(codeVo.getCode());
            itCustomerService.createCustomer(vo);
        }
    }
}
